<template>
	<view class="container">
		<!-- 侧边导航栏 -->
		<view class="sidebar">
			<view v-for="(item, index) in navItems" :key="index"
				:class="['nav-item', { active: activeIndex === index }]" @click="handleNavClick(index, item.path)">
				{{ item.name }}
			</view>
		</view>
		<view class='content'>
			<view class="refund-page">
				<view class="search-bar">
					<picker mode="selector" :range="categories" @change="onCategoryChange">
						<view class="search-bar-item">
							<text class="search-bar-label">选择类别：</text>
							<text class="search-bar-value">{{ selectedCategory }}</text>
						</view>
					</picker>
					<input class="search-bar-item" type="text" placeholder="请输入病历ID" v-model="searchText" />
					<button class="search-bar-item search-bar-button" @click="onSearch">查询</button>
				</view>
				<view class="table-header">
					<view class="table-cell">名称</view>
					<view class="table-cell">规格</view>
					<view class="table-cell">数量</view>
					<view class="table-cell">单价</view>
					<view class="table-cell">小计</view>
					<view class="table-cell">类别</view>
				</view>
				<view class="table-row" v-for="(item, index) in filteredItems" :key="index">
					<view class="table-cell">{{ item.name }}</view>
					<view class="table-cell">{{ item.spec }}</view>
					<view class="table-cell">{{ item.quantity }}</view>
					<view class="table-cell">{{ item.price }}</view>
					<view class="table-cell">{{ item.quantity * item.price }}</view>
					<view class="table-cell">{{ item.category }}</view>
				</view>
				<view class="total-amount">
					<text class="total-amount-label">总金额：</text>
					<text class="total-amount-value">{{ totalAmount }}</text>
					<button class="refund-button" @click="onRefund">退费</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				items: [],
				categories: ['药品', '检查检验'],
				selectedCategory: '药品',
				searchText: '',
				// 导航栏项
				navItems: [{
						name: '处方管理',
						path: '/pages/Prescription/Prescription'
					},
					{
						name: '药品管理',
						path: '/pages/drugManagement/drugManagement'
					},
					{
						name: '收费管理',
						path: '/pages/Cachier/Cachier'
					},
					{
						name: '退费管理',
						path: '/pages/Cachier/Refund'
					}
				],
				// 当前激活的导航项索引
				activeIndex: 3,
				//链接相关
				baseUrl: "http://172.22.224.1:8080"
			};
		},
		computed: {
			filteredItems() {
				if (!Array.isArray(this.items)) {
					return []; // 如果 this.items 不是数组，返回空数组
				}
				return this.items.filter(item => {
					return item.category === this.selectedCategory;
				});
			},
			totalAmount() {
				if (!Array.isArray(this.filteredItems)) {
					return 0; // 如果 filteredItems 不是数组，返回 0
				}
				return this.filteredItems.reduce((total, item) => {
					return total + item.quantity * item.price;
				}, 0);
			}
		},
		methods: {
			onCategoryChange(event) {
				this.selectedCategory = this.categories[event.detail.value];
			},
			async onSearch() {
				if (!this.searchText) {
					uni.showToast({
						title: '请输入病历ID',
						icon: 'none'
					});
					return;
				}

				try {
					const response = await uni.request({
						url: this.baseUrl + '/cashier/getAllPaidByDiagId', // 接口地址
						method: 'POST',
						data: {
							diagId: this.searchText
						},
						header: {
							'Content-Type': 'application/json'
						}
					});

					if (response.statusCode === 200 && response.data) {
						this.items = response.data.data;
					} else {
						uni.showToast({
							title: '查询失败，请重试',
							icon: 'none'
						});
					}
				} catch (error) {
					uni.showToast({
						title: '请求失败，请检查网络',
						icon: 'none'
					});
					console.error('请求失败:', error);
				}
			},
			async onRefund() {
				if (!this.searchText) {
					uni.showToast({
						title: '请输入病历ID',
						icon: 'none'
					});
					return;
				}

				if (this.filteredItems.length === 0) {
					uni.showToast({
						title: '没有可退费的项目',
						icon: 'none'
					});
					return;
				}

				try {
					// 发起退费请求，传入病历 ID 和当前筛选的类别
					const response = await uni.request({
						url: this.baseUrl + '/cashier/refund', // 退费接口地址
						method: 'POST',
						data: {
							id: this.searchText, // 病历 ID
							category: this.selectedCategory // 当前筛选的类别
						},
						header: {
							'Content-Type': 'application/json'
						}
					});

					if (response.statusCode === 200 && response.data.code === 200) {
						uni.showToast({
							title: '退费成功',
							icon: 'success'
						});
						// 退费成功后刷新数据
						this.onSearch();
					} else {
						uni.showToast({
							title: response.data.message || '退费失败',
							icon: 'none'
						});
					}
				} catch (error) {
					uni.showToast({
						title: '请求失败，请检查网络',
						icon: 'none'
					});
					console.error('退费请求失败:', error);
				}
			},
			// 处理导航项点击事件
			handleNavClick(index, path) {
				this.activeIndex = index;
				// 跳转到对应页面
				uni.redirectTo({
					url: path
				});
			}
		},
	};
</script>

<style>
	.container {
		display: flex;
		height: 100vh;
	}

	.sidebar {
		width: 200px;
		background-color: #f0f0f0;
		padding: 20px;
	}

	.nav-item {
		padding: 10px;
		margin-bottom: 10px;
		cursor: pointer;
		border-radius: 5px;
		transition: background-color 0.3s;
	}

	.nav-item.active {
		background-color: #007aff;
		color: white;
	}

	.nav-item:hover {
		background-color: #e0e0e0;
	}

	.content {
		flex: 1;
		padding: 20px;
		background-color: #fff;
	}

	.refund-page {
		font-family: Arial, sans-serif;
		width: 100%;
		max-width: 800px;
		margin: 0 auto;
		padding: 20px;
		box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
	}

	.search-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
	}

	.search-bar-item {
		flex: 1;
		margin: 0 10px;
		padding: 10px;
		border: 1px solid #ccc;
		border-radius: 5px;
		display: flex;
		align-items: center;
	}

	.search-bar-label {
		font-weight: bold;
		margin-right: 10px;
	}

	.search-bar-value {
		flex: 1;
		text-align: right;
	}

	.search-bar-button {
		flex: 0.1;
		padding: 10px 20px;
		background-color: #007bff;
		color: #fff;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.table-header,
	.table-row {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10px 0;
		border-bottom: 1px solid #ddd;
	}

	.table-cell {
		flex: 1;
		text-align: center;
		padding: 0 10px;
	}

	.table-header {
		font-weight: bold;
		background-color: #5df574;
	}

	.table-row:nth-child(odd) {
		background-color: #f9f9f9;
	}

	.total-amount {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20px;
	}

	.total-amount-label {
		font-weight: bold;
	}

	.total-amount-value {
		flex: 1;
		text-align: left;
	}

	.refund-button {
		flex: 0.1;
		padding: 10px 20px;
		background-color: #ff0000;
		color: #fff;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>